<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author LYS
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #toolbar>
        <a-button type="primary" @click="handleForm({projectno:projectno})" v-auth="'proj:weekly:pmWeekly:edit'">
          <Icon icon="fluent:add-12-filled" /> {{ t('周报') }}
        </a-button>
      </template>
      <template #firstColumn="{ record }">
        <a @click="handleForm({ id: record.id })">
          {{ record.projectno }}
        </a>
      </template>
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
  </div>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsProjWeeklyPmWeeklyList',
  });
</script>
<script lang="ts" setup>
  import { defineComponent ,watch,ref} from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import { pmWeeklyDelete, pmWeeklyListData } from '/@/api/proj/weekly/pmWeekly';
  import { officeTreeData } from '/@/api/sys/office';
  import { useDrawer } from '/@/components/Drawer';
  import { FormProps } from '/@/components/Form';
  import InputForm from './form.vue';

  const { t } = useI18n('proj.weekly.pmWeekly');
  const { showMessage } = useMessage();
  const getTitle = {
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: t('周报管理'),
  };
  const projectno = ref<string>('');
  const props = defineProps({
    projectno: String,
  });
  watch(
    () => props.projectno,
    async () => {
      projectno.value = props.projectno
      console.log(projectno.value,'projectno.value==698');
    },
    {
      immediate: true,
    },
  );

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 90,
    schemas: [
      // {
      //   label: t('项目编号'),
      //   field: 'projectno',
      //   component: 'Input',
      //   ifShow:false
      // },
      // {
      //   label: t('项目名称'),
      //   field: 'projectname',
      //   component: 'Input',
      //   ifShow:false
      // },
      {
        label: t('开始日期'),
        field: 'starttime',
        component: 'DatePicker',
        componentProps: {
          format: 'YYYY-MM-DD',
          valueFormat: 'YYYY-MM-DD',
          // showTime: { format: 'HH:mm' },
        },
      },
      {
        label: t('结束日期'),
        field: 'endtime',
        component: 'DatePicker',
        componentProps: {
          format: 'YYYY-MM-DD',
          valueFormat: 'YYYY-MM-DD',
          // showTime: { format: 'HH:mm' },
        },
      },
      // {
      //   label: t('承包商'),
      //   field: 'contractor',
      //   component: 'Input',
      // },
      // {
      //   label: t('本周事项'),
      //   field: 'remarks',
      //   component: 'Input',
      // },
      // {
      //   label: t('填报人'),
      //   field: 'createByName',
      //   component: 'TreeSelect',
      //   componentProps: {
      //     api: officeTreeData,
      //     params: { isLoadUser: true, userIdPrefix: '' },
      //     canSelectParent: false,
      //     allowClear: true,
      //   },
      // },
    ],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('项目编号'),
      dataIndex: 'projectno',
      key: 'a.projectno',
      sorter: true,
      width: 180,
      align: 'left',
      slot: 'firstColumn',
    },
    {
      title: t('项目名称'),
      dataIndex: 'projectname',
      key: 'a.name',
      sorter: true,
      width: 220,
      align: 'left',
    },
   
    {
      title: t('开始日期'),
      dataIndex: 'starttime',
      key: 'a.starttime',
      sorter: true,
      width: 130,
      align: 'center',
    },
  
    {
      title: t('结束日期'),
      dataIndex: 'endtime',
      key: 'a.endtime',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('承包商'),
      dataIndex: 'contractor',
      key: 'a.contractor',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('施工内容'),
      dataIndex: 'remarks',
      key: 'a.remarks',
      sorter: true,
      width: 220,
      align: 'left',
    },
    {
      title: t('填报人'),
      dataIndex: 'createByName',
      key: 'a.create_by_name',
      sorter: true,
      width: 130,
      align: 'left',
    },
     {
      title: t('填报时间'),
      dataIndex: 'createDate',
      key: 'a.createtime',
      sorter: true,
      width: 130,
      align: 'center',
    },
  ];

  const actionColumn: BasicColumn = {
    width: 160,
    actions: (record: Recordable) => [
      {
        icon: 'clarity:note-edit-line',
        title: t('编辑周报'),
        onClick: handleForm.bind(this, { id: record.id}),
        auth: 'proj:weekly:pmWeekly:edit',
      },
      {
        icon: 'ant-design:delete-outlined',
        color: 'error',
        title: t('删除周报'),
        popConfirm: {
          title: t('是否确认删除周报'),
          confirm: handleDelete.bind(this, { id: record.id }),
        },
        auth: 'proj:weekly:pmWeekly:delete',
      },
    ],
  };

  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerTable, { reload, getForm }] = useTable({
    api: pmWeeklyListData,
    beforeFetch: (params) => {
      params.projectno = props.projectno
      return params;
    },
    columns: tableColumns,
    actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
  });

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }

  async function handleDelete(record: Recordable) {
    const res = await pmWeeklyDelete(record);
    showMessage(res.message);
    handleSuccess();
  }

  function handleSuccess() {
    reload();
  }
</script>
